<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Open RAR archive</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="rararchive.issolid.html">« RarArchive::isSolid</a></li>
      <li style="float: right;"><a href="rararchive.setallowbroken.html">RarArchive::setAllowBroken »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.rararchive.html">RarArchive</a></li>
    <li>Open RAR archive</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="rararchive.open" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">RarArchive::open</h1>
  <h1 class="refname">rar_open</h1>
  <p class="verinfo">(PECL rar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">RarArchive::open</span> -- <span class="refname">rar_open</span> &mdash; <span class="dc-title">Open RAR archive</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-rararchive.open-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格 (method):</p>
   <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>RarArchive::open</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code><span class="initializer"> = NULL</span></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$volume_callback</code><span class="initializer"> = NULL</span></span>): <span class="type"><span class="type"><a href="class.rararchive.html" class="type RarArchive">RarArchive</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">过程化风格:</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>rar_open</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code><span class="initializer"> = NULL</span></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$volume_callback</code><span class="initializer"> = NULL</span></span>): <span class="type"><span class="type"><a href="class.rararchive.html" class="type RarArchive">RarArchive</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Open specified RAR archive and return <span class="type"><a href="class.rararchive.html" class="type RarArchive">RarArchive</a></span> instance representing it.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    If opening a multi-volume archive, the path of the first volume should be passed as the first parameter.
    Otherwise, not all files will be shown. This is by design.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-rararchive.open-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">filename</code></dt>

     <dd>

      <p class="para">
       Path to the Rar archive.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">password</code></dt>

     <dd>

      <p class="para">
       A plain password, if needed to decrypt the headers. It will also be used by default
       if encrypted files are found. Note that the files may have
       different passwords in respect to the headers and among them.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">volume_callback</code></dt>

     <dd>

      <p class="para">
       A function that receives one parameter – the path of the volume
       that was not found – and returns a string with the correct path
       for such volume or <strong><code>null</code></strong> if such volume does
       not exist or is not known. The programmer should ensure the
       passed function doesn&#039;t cause loops as this function is called
       repeatedly if the path returned in a previous call did not
       correspond to the needed volume. Specifying this parameter omits
       the notice that would otherwise be emitted whenever a volume is
       not found; an implementation that only returns <strong><code>null</code></strong>
       can therefore be used to merely omit such notices.
      </p>
     </dd>

    
   </dl>

  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    Prior to version 2.0.0, this function would not handle relative
    paths correctly. Use <span class="function"><a href="function.realpath.html" class="function">realpath()</a></span> as a workaround.
   </p>
  </div>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-rararchive.open-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the requested <span class="type"><a href="class.rararchive.html" class="type RarArchive">RarArchive</a></span> instance  或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-rararchive.open-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL rar 3.0.0</td>
       <td>
        <code class="parameter">volume_callback</code> was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-rararchive.open-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-807">
    <p><strong>示例 #1 面向对象风格</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$rar_arch&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">RarArchive</span><span style="color: #007700">::</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">'encrypted_headers.rar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'samplepassword'</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$rar_arch&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Failed&nbsp;opening&nbsp;file"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$rar_arch</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getEntries</span><span style="color: #007700">();<br />if&nbsp;(</span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Failed&nbsp;fetching&nbsp;entries"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Found&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$entries</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;files.\n"</span><span style="color: #007700">;<br /><br />if&nbsp;(empty(</span><span style="color: #0000BB">$entries</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"No&nbsp;valid&nbsp;entries&nbsp;found."</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">reset</span><span style="color: #007700">(</span><span style="color: #0000BB">$entries</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">getStream</span><span style="color: #007700">();<br />if&nbsp;(</span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Failed&nbsp;opening&nbsp;first&nbsp;file"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$rar_arch</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Content&nbsp;of&nbsp;first&nbsp;one&nbsp;follows:\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程的输出类似于：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
Found 2 files.
Content of first one follows:
Encrypted file 1 contents.
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-808">
    <p><strong>示例 #2 过程化风格</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$rar_arch&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rar_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'encrypted_headers.rar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'samplepassword'</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$rar_arch&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Failed&nbsp;opening&nbsp;file"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rar_list</span><span style="color: #007700">(</span><span style="color: #0000BB">$rar_arch</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Failed&nbsp;fetching&nbsp;entries"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Found&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$entries</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;files.\n"</span><span style="color: #007700">;<br /><br />if&nbsp;(empty(</span><span style="color: #0000BB">$entries</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"No&nbsp;valid&nbsp;entries&nbsp;found."</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">reset</span><span style="color: #007700">(</span><span style="color: #0000BB">$entries</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">getStream</span><span style="color: #007700">();<br />if&nbsp;(</span><span style="color: #0000BB">$stream&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Failed&nbsp;opening&nbsp;first&nbsp;file"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">rar_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$rar_arch</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Content&nbsp;of&nbsp;first&nbsp;one&nbsp;follows:\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-809">
    <p><strong>示例 #3 Volume Callback</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;In&nbsp;this&nbsp;example,&nbsp;there's&nbsp;a&nbsp;volume&nbsp;named&nbsp;multi_broken.part1.rar<br />&nbsp;*&nbsp;whose&nbsp;next&nbsp;volume&nbsp;is&nbsp;named&nbsp;multi.part2.rar&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">resolve</span><span style="color: #007700">(</span><span style="color: #0000BB">$vol</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/_broken/'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$vol</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'_broken'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$vol</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$rar_file1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rar_open</span><span style="color: #007700">(</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">).</span><span style="color: #DD0000">'/multi_broken.part1.rar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'resolve'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$entry&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$rar_file1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getEntry</span><span style="color: #007700">(</span><span style="color: #DD0000">'file2.txt'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$entry</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">extract</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"/temp_file2.txt"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-rararchive.open-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="wrappers.rar.html" class="link"><code class="literal">rar://</code> wrapper</a></li>
   </ul>
  </p>
 </div>



</div></div></div></body></html>